package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.activity.model.SeckillGoods;
import com.atguigu.gmall.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-01-30 15:59
 */
@Controller
public class SeckillController {


    @Autowired
    private ActivityFeignClient activityFeignClient;


    /**
     * 秒杀首页
     *
     * @param model
     * @return
     */
    @GetMapping("/seckill.html")
    public String seckillIndex(Model model) {
        List<SeckillGoods> list = activityFeignClient.findAll().getData();
        model.addAttribute("list", list);
        return "seckill/index";
    }


    /**
     * 秒杀商品详情
     *
     * @param skuId
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    private String getSckillGoods(@PathVariable("skuId") Long skuId, Model model) {
        SeckillGoods seckillGoods = activityFeignClient.getSeckillGoods(skuId).getData();
        model.addAttribute("item", seckillGoods);
        return "seckill/item";

    }


    /**
     * 渲染排队页面
     *
     * @param skuId
     * @param skuIdStr
     * @param model
     * @return
     */
    @GetMapping("/seckill/queue.html")
    public String seckillQueueHtml(String skuId, String skuIdStr, Model model) {
        model.addAttribute("skuId", skuId);
        model.addAttribute("skuIdStr", skuIdStr);
        return "seckill/queue";
    }

    /**
     * 渲染秒杀订单确认页面
     *
     * @param model
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model) {
        Result<Map<String, Object>> result = activityFeignClient.seckillTradeData();
        model.addAllAttributes(result.getData());
        return "seckill/trade";
    }
}
